package offer.algorithm;

/**
 * Description：
 * Author: zhangc
 * Date：2017/7/18 18:53
 */
public class ReverseArray8 {

    public static <T extends Comparable<? super T>> T getMin(T[] array) {
        if (array.length == 0)
            return null;
        else if (array.length == 1 || array[0].compareTo(array[array.length-1]) <= 0 )
            return array[ 0 ];
        else {
            int midIndex = array.length >> 1;
            T middle = array[midIndex];
            int retIndex = midIndex;
            if (array[0].compareTo(middle) < 0){
                for (int i = array.length-1; i >midIndex ; --i) {
                    if(array[i].compareTo(middle) >= 0) {
                        retIndex = i;
                        break;
                    }
                }
                retIndex++;
            }else{
                for (int i = 0; i <midIndex ; ++i) {
                    if(array[i].compareTo(middle) <= 0) {
                        retIndex = i;
                        break;
                    }
                }
            }
            return array[retIndex];
        }

    }


}
